<template>
  <div class="fr-page">
    <div class="fr-tool">
      <el-button plain size="small" @click="formOperate('add', '')">新增</el-button>
      <el-button plain size="small" @click="previewFor1x" :disabled="tableSelectionList.length !== 1">
        预览代码(1x)
      </el-button>
      <el-button plain size="small" @click="previewForIot1x" :disabled="tableSelectionList.length !== 1">
        预览代码(Iot1x)
      </el-button>
      <el-button plain size="small" @click="previewForJeeplus8x" :disabled="tableSelectionList.length !== 1">
        预览代码(Jeeplus8x)
      </el-button>
      <!--      <el-button plain size="small" @click="downloadForJeeplus9x" :disabled="tableSelectionList.length !== 1">
              下载代码(Jeeplus9x)
            </el-button>-->
      <el-button plain size="small" @click="previewForJeeplus9x" :disabled="tableSelectionList.length !== 1">
        预览代码(Jeeplus9x)
      </el-button>
      <el-button-group></el-button-group>
    </div>
    <div class="fr-content">
      <el-table :data="tableList" :v-loading="loading" @selection-change="handelTableSelectionChange">
        <el-table-column type="selection"/>
        <el-table-column
          prop="tableName"
          label="数据库表名称"/>
        <el-table-column
          prop="entityName"
          label="实体类名称"/>
        <el-table-column
          label="操作">
          <template slot-scope="scope">
            <el-link @click="formOperate('edit', scope.row.id)">修改</el-link>
          </template>
        </el-table-column>
      </el-table>
      <vxe-pager :page-size.sync="tablePage.size" :total="tablePage.total"
                 :current-page.sync="tablePage.current"
                 :page-sizes="[10, 20, 100, 1000, {label: '全量数据', value: 1000000}]"
                 :layouts="['PrevPage', 'JumpNumber', 'NextPage', 'FullJump', 'Sizes', 'Total']"
                 @page-change="refreshList"></vxe-pager>
    </div>
    <gen-code-form :visible.sync="formDialog.visible" :method="formDialog.method" :gen-id="formDialog.dataId"
                   @submit-success="refreshList"/>

    <gen-code-preview :visible.sync="previewDialog.visible" method="edit" :gen-id="previewDialog.dataId"
                      :version="previewDialog.version"/>
  </div>
</template>

<script>
import GenCodeForm from '@views/modules/generator/GenCodeForm.vue'
import GenCodePreview from '@views/modules/generator/GenCodePreview.vue'

export default {
  name: 'GenCodeList',
  components: {
    GenCodePreview,
    GenCodeForm
  },
  data () {
    return {
      process: {
        listLoading: false
      },
      tableList: [],
      tableSelectionList: [],
      formDialog: {
        visible: false,
        method: '',
        dataId: ''
      },
      tablePage: {
        size: 10,
        total: 0,
        current: 1,
        orders: [{
          column: 'update_time',
          asc: false
        }]
      },
      previewDialog: {
        dataId: '',
        visible: false,
        version: ''
      }
    }
  },
  mounted () {
    this.refreshList()
  },
  computed: {
    loading () {
      return this.process.listLoading
    }
  },
  methods: {
    refreshList () {
      this.process.listLoading = true
      this.$http.get('/generator/code/page', { params: { ...this.tablePage } }).then(({ data }) => {
        this.tableList = data.page.records
        this.$override(this.tablePage, data.page)
      })
    },
    formOperate (method, genId) {
      this.formDialog.dataId = genId
      this.formDialog.method = method
      this.formDialog.visible = true
    },
    handelTableSelectionChange (val) {
      this.tableSelectionList = val
    },
    downloadForJeeplus8x (id) {
      this.process.listLoading = true
      this.$http.download(`/generator/code/downloadForJeeplus8x?id=${id}`).then((resp) => {
      })
    },
    downloadForJeeplus9x () {
      this.process.listLoading = true
      const id = this.tableSelectionList[0].id
      this.$http.download(`/generator/code/downloadForJeeplus9x?id=${id}`).then((resp) => {
      })
    },
    previewFor1x () {
      const id = this.tableSelectionList[0].id
      this.previewDialog.dataId = id
      this.previewDialog.version = '1x'
      this.previewDialog.visible = true
    },
    previewForIot1x () {
      const id = this.tableSelectionList[0].id
      this.previewDialog.dataId = id
      this.previewDialog.version = 'iot1x'
      this.previewDialog.visible = true
    },
    previewForJeeplus9x () {
      const id = this.tableSelectionList[0].id
      this.previewDialog.dataId = id
      this.previewDialog.version = 'jeeplus9x'
      this.previewDialog.visible = true
    },
    previewForJeeplus8x () {
      const id = this.tableSelectionList[0].id
      this.previewDialog.dataId = id
      this.previewDialog.version = 'jeeplus8x'
      this.previewDialog.visible = true
    }
  }
}
</script>

<style scoped lang="scss">
</style>
